package com.dingwei.lesyndrome.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.adapter.CircleCommentAdapter;
import com.dingwei.lesyndrome.adapter.GamCirclePicAdapter;
import com.dingwei.lesyndrome.app.MyApplication;
import com.dingwei.lesyndrome.bean.CirclCommentListBean;
import com.dingwei.lesyndrome.bean.CircleDetailsBean;
import com.dingwei.lesyndrome.mvp.presenter.CircleDetailsPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.dingwei.lesyndrome.utils.FindCommentDialog;
import com.jude.easyrecyclerview.decoration.SpaceDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/3/6.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class CircleDetailsActivity extends BaseActivity implements Contract.CircleDetailsView {

    CircleDetailsPresenter circleDetailsPresenter = new CircleDetailsPresenter(this);
    @Bind(R.id.no_net_btn)
    TextView noNetBtn;
    @Bind(R.id.no_net_layout)
    RelativeLayout noNetLayout;
    @Bind(R.id.img_head)
    ImageView imgHead;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_content)
    TextView tvContent;
    @Bind(R.id.grid_pics)
    RecyclerView gridPics;
    @Bind(R.id.img_top)
    ImageView imgTop;
    @Bind(R.id.fl_video)
    FrameLayout flVideo;
    @Bind(R.id.tv_watch)
    TextView tvWatch;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.img_like)
    ImageView imgLike;
    @Bind(R.id.img_comment)
    ImageView imgComment;
    @Bind(R.id.ll_likes)
    LinearLayout llLikes;
    @Bind(R.id.tv_likes)
    TextView tvLikes;
    @Bind(R.id.ll_likes_content)
    LinearLayout llLikesContent;
    @Bind(R.id.list_comments)
    RecyclerView listComments;
    @Bind(R.id.img_no_data)
    ImageView imgNoData;
    @Bind(R.id.tv_no_data)
    TextView tvNoData;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;


    GamCirclePicAdapter adapter;
    CircleCommentAdapter commentAdapter;
    CircleDetailsBean bean;
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.img_collect)
    ImageView imgCollect;
    @Bind(R.id.img_report)
    ImageView imgReport;
    private String id = "";

    @Override
    protected int getContentViewId() {
        return R.layout.activity_circle_detail;
    }

    @Override
    protected void initData() {
        circleDetailsPresenter.initData(id);
    }

    @Override
    protected void initView() {
        id = getIntent().getStringExtra("id");
        adapter = new GamCirclePicAdapter(BaseActivity.context);
        GridLayoutManager layoutManager = new GridLayoutManager(BaseActivity.context, 4);
        layoutManager.setSpanSizeLookup(adapter.obtainGridSpanSizeLookUp(4));
        SpaceDecoration itemDecoration = new SpaceDecoration((int) DisplayUtil.dip2px(BaseActivity.context, 10));//参数是距离宽度
        itemDecoration.setPaddingEdgeSide(false);//是否为左右2边添加padding.默认true.
        itemDecoration.setPaddingStart(false);//是否在给第一行的item添加上padding(不包含header).默认true.
        itemDecoration.setPaddingHeaderFooter(false);//是否对Header于Footer有效,默认false.
        gridPics.setLayoutManager(layoutManager);
        gridPics.setAdapter(adapter);
        gridPics.setNestedScrollingEnabled(false);
        gridPics.addItemDecoration(itemDecoration);


        commentAdapter = new CircleCommentAdapter(BaseActivity.context);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        listComments.setLayoutManager(linearLayoutManager);
        listComments.setNestedScrollingEnabled(false);
        listComments.setAdapter(commentAdapter);


        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                circleDetailsPresenter.initCommentList(id, page, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                circleDetailsPresenter.initCommentList(id, page, true, false);

            }
        });

        commentAdapter.onCommentInterfaceListener(new CircleCommentAdapter.onCommentInterFace() {
            @Override
            public void onComment(int position) {

            }

            @Override
            public void onHead(int position) {
                CircleDetailsBean.DataBean.CommentListBean bean = commentAdapter.getItem(position);
                intent  = new Intent(BaseActivity.context,HisCircleActivity.class);
                intent.putExtra("mid",bean.member_id);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void setData(CircleDetailsBean bean) {
        this.bean = bean;
        Glide.with(context)
                .load(bean.data.showHportrait)
                .apply(MyApplication.headOptions)
                .into(imgHead);
        tvName.setText(bean.data.showName);
        tvTime.setText(bean.data.showTime);
        tvContent.setText(bean.data.content);
        tvWatch.setText("浏览" + bean.data.views + "次");
        tvAddress.setText(bean.data.address);
        if (TextUtils.isEmpty(bean.data.video)) {
            flVideo.setVisibility(View.GONE);
        } else {
            flVideo.setVisibility(View.VISIBLE);
            Glide.with(BaseActivity.context)
                    .load(bean.data.vimg)
                    .apply(MyApplication.options)
                    .into(imgTop);
        }


        if (bean.data.imgList != null && bean.data.imgList.size() > 0) {
            gridPics.setVisibility(View.VISIBLE);
            adapter.clear();
            for (int i = 0; i < bean.data.imgList.size(); i++) {
                adapter.add(bean.data.imgList.get(i).showFile);
            }
        } else {
            gridPics.setVisibility(View.GONE);
        }


        if (bean.data.isAgree) {
            imgLike.setImageResource(R.drawable.ic_circle_like);
        } else {
            imgLike.setImageResource(R.drawable.ic_circle_normal);
        }

        if (bean.data.isCollect) {
            imgCollect.setImageResource(R.drawable.pic23);
        } else {
            imgCollect.setImageResource(R.drawable.pic65);
        }
        if (Integer.valueOf(bean.data.agreeList.size()) > 0) {
            llLikesContent.setVisibility(View.VISIBLE);
        } else {
            llLikesContent.setVisibility(View.GONE);
        }
        tvLikes.setText("等" + bean.data.agreeList.size() + "个人觉得很赞");
        llLikes.removeAllViews();
        for (int i = 0; i < bean.data.agreeList.size(); i++) {
            ImageView imagView = new ImageView(CircleDetailsActivity.this);
            int width = DisplayUtil.dip2px(BaseActivity.context, 18);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width, width);
            lp.setMargins(0, 0, DisplayUtil.dip2px(BaseActivity.context, 6), 0);
            imagView.setLayoutParams(lp);
            imagView.setImageResource(R.drawable.head_default);
            Glide.with(BaseActivity.context)
                    .load(bean.data.agreeList.get(i).showHportrait)
                    .apply(MyApplication.headOptions)
                    .into(imagView);
            imagView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            llLikes.addView(imagView);
        }

        commentAdapter.clear();
        commentAdapter.addAll(bean.data.commentList);

        showData("success");

    }

    @Override
    public void onFail() {
        showData("fail");
    }

    @Override
    public void onRefresh(CirclCommentListBean bean) {
        commentAdapter.clear();
        commentAdapter.addAll(bean.data);
        refreshLayout.finishRefresh();
        showData("success");
    }

    @Override
    public void onLoadMore(CirclCommentListBean bean) {
        commentAdapter.addAll(bean.data);
        refreshLayout.finishLoadmore();
        showData("success");
    }

    @Override
    public void likeSuccess() {
        bean.data.isAgree = !bean.data.isAgree;
        if (bean.data.isAgree) {
            imgLike.setImageResource(R.drawable.ic_circle_like);
        } else {
            imgLike.setImageResource(R.drawable.ic_circle_normal);
        }
        page = 1;
        circleDetailsPresenter.initData(id);
    }

    @Override
    public void CommentSuccess() {
        page = 1;
        circleDetailsPresenter.initData(id);
    }

    @Override
    public void collectSuccess() {
        bean.data.isCollect = !bean.data.isCollect;
        if (bean.data.isCollect) {
            imgCollect.setImageResource(R.drawable.pic23);
        } else {
            imgCollect.setImageResource(R.drawable.pic65);
        }
    }


    public void showData(String state) {
        if (state.equals("success")) {
            noNetLayout.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
            if (commentAdapter.getAllData() != null && commentAdapter.getAllData().size() > 0) {
                listComments.setVisibility(View.VISIBLE);
                noDataLayout.setVisibility(View.GONE);
            } else {
                listComments.setVisibility(View.GONE);
                noDataLayout.setVisibility(View.VISIBLE);
            }
        } else {
            refreshLayout.setVisibility(View.GONE);
            noNetLayout.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.fl_video, R.id.img_like, R.id.img_comment,
            R.id.no_net_btn,R.id.img_back,R.id.img_collect,
            R.id.img_report,R.id.img_head})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fl_video:
                Intent intent = new Intent(BaseActivity.context, VideoPlayerActivity.class);
                intent.putExtra("url", bean.data.video);
                intent.putExtra("face", bean.data.vimg);
                startActivity(intent);
                break;
            case R.id.img_like:
                if (BaseActivity.value.getIsLogin()){
                    circleDetailsPresenter.like(id);
                }else {
                    intent = new Intent(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.img_comment:
                if (BaseActivity.value.getIsLogin()) {
                    FindCommentDialog dialog = new FindCommentDialog(CircleDetailsActivity.this, null);
                    dialog.show();
                    dialog.setClick(new FindCommentDialog.MyClick() {
                        @Override
                        public void onCommit(String content) {
                            circleDetailsPresenter.comment(id, content);
                        }
                    });
                } else {
                    intent = new Intent(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.no_net_btn:
                page = 1;
                circleDetailsPresenter.initData(id);
                break;
            case R.id.img_back:
                finish();
                break;
            case R.id.img_collect:
                if (BaseActivity.value.getIsLogin()){
                    circleDetailsPresenter.collect(id);
                }else {
                    intent = new Intent(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }
                break;
            case R.id.img_report:
                if (BaseActivity.value.getIsLogin()){
                    intent  = new Intent(BaseActivity.context,CircleReportActivity.class);
                    intent.putExtra("bean",bean);
                    startActivity(intent);
                }else {
                    intent = new Intent(BaseActivity.context, LoginActivity.class);
                    startActivity(intent);
                }

                break;
            case R.id.img_head:
                intent  = new Intent(BaseActivity.context,HisCircleActivity.class);
                intent.putExtra("mid",bean.data.member_id);
                startActivity(intent);
                break;
        }
    }
}
